package dmsxl.dandiaozhan;

import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;

/**
 * Author: Zhang Dongwei
 * Date: 2023/9/27 11:47
 */
public class ddz3_503 {
    public static void main(String[] args) {
        int[] nums = {1, 2, 1};
        System.out.println(Arrays.toString(nextGreaterElements(nums)));
    }

    //    自己写的，通过
    public static int[] nextGreaterElements(int[] nums) {
        int[] nums1 = new int[2 * nums.length];
        for (int i = 0; i < nums.length; i++) {
            nums1[i] = nums[i];
        }
        for (int i = 0; i < nums.length; i++) {
            nums1[nums.length + i] = nums[i];
        }
        int[] res1 = new int[nums1.length];
        int[] res = new int[nums.length];
        Arrays.fill(res1, -1);
        Deque<Integer> stack = new LinkedList<>();
        for (int i = 0; i < nums1.length; i++) {
            while (!stack.isEmpty() && nums1[i] > nums1[stack.peek()]) {
                res1[stack.peek()] = nums1[i];
                stack.pop();
            }
            stack.push(i);

        }
        for (int i = 0; i < nums.length; i++) {
            res[i] = res1[i];
        }
        return res;
    }
}
